package org.zjt.config.operation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zjt.config.exception.RemoteConfigInitException;
import org.zjt.config.properties.ApplicationProperties;
import org.zjt.config.properties.RemotePropertiesHolder;
import org.zjt.config.register.ConfigNodeInfo;
import org.zjt.config.register.Register;

import java.util.Set;

/**
 * @author <a href="juntao.zhang@bkjk.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.config.config
 * @date 2017/12/14 16:40
 * @see
 */
public class UnifiedConfigOperation implements IUnifiedConfigOperation{

    private static final Logger LOGGER = LoggerFactory.getLogger(UnifiedConfigOperation.class);

    private Register register;

    private String configServerDir;

    private ApplicationProperties applicationProperties;

    private String configClientDir;

    public Register getRegister() {
        return register;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

    public String getConfigServerDir() {
        return configServerDir;
    }

    public void setConfigServerDir(String configServerDir) {
        this.configServerDir = configServerDir;
    }

    public String getConfigClientDir() {
        return configClientDir;
    }

    public void setConfigClientDir(String configClientDir) {
        this.configClientDir = configClientDir;
    }

    public ApplicationProperties getApplicationProperties() {
        return applicationProperties;
    }

    public void setApplicationProperties(ApplicationProperties applicationProperties) {
        this.applicationProperties = applicationProperties;
    }

    @Override
    public void pullConfigInfo() {
        Set<ConfigNodeInfo> configNodeInfos = register.pullConfigInfo();
        boolean loadResource = false;
        for (ConfigNodeInfo configNodeInfo: configNodeInfos){
            try {
                loadResource = RemotePropertiesHolder.getInstance().loadResource(configNodeInfo, applicationProperties);
                if (loadResource) break;
            }catch (Exception e){
                LOGGER.debug("加载配置异常",e);
            }
        }
        if (!loadResource)
            throw new RemoteConfigInitException("load remote properties fail");

        //发布client节点

        //监控client节点，更新config配置

        //

    }

    @Override
    public void pullConfigInfoByKey(String key) {

    }
}
